package com.fjzx.androidframe.network;

import android.arch.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 基础ViewModel类，封装了取消该ViewModel中所有网络请求的方法，
 * 子类需要调用cacheCall()将网络请求的Call实例缓存到List中
 *
 * @author xiongyongcong
 */
public class BaseViewModel extends ViewModel {
    private List<Call> mHttpCalls = new ArrayList<>();

    public void cacheCall(Call call) {
        mHttpCalls.add(call);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (mHttpCalls != null && !mHttpCalls.isEmpty()) {
            for (Call call : mHttpCalls) {
                call.cancel();
            }
            mHttpCalls.clear();
            mHttpCalls = null;
        }
    }
}
